<%@ page language="java" import="java.util.*,javaNews.*,java.io.*" pageEncoding="utf-8"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>




<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'checkModify.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  
       <%!
  String newName(String name){
  int pos=name.lastIndexOf('.');
  String prefix=name.substring(pos==-1?0:pos);
  return UUID.randomUUID()+prefix;
  }
   %>
   
      
      <%
      request.setCharacterEncoding("utf-8");
      String uploadFileName="";
      String fieldName="";
      boolean isMultipart=ServletFileUpload.isMultipartContent(request);
      String uploadFilePath=request.getSession().getServletContext().getRealPath("upload/");
      if(isMultipart){
      FileItemFactory factory=new DiskFileItemFactory();
      ServletFileUpload upload=new ServletFileUpload(factory);
      try{
      List<FileItem> items=upload.parseRequest(request);
      Iterator<FileItem> iter=items.iterator();
      
      
              int ntid=0;
              int nid=0;
              String title=null;
              String author=null;
              String summary=null;
              String content=null;
              String npicPath=null;
      
      while(iter.hasNext()){
          FileItem item=(FileItem)iter.next();
          if(item.isFormField()){
          fieldName=item.getFieldName();
          
            if(fieldName.equals("ntid")){
          out.println("<p>类别编号："+item.getString("utf-8")+"</p>");
          ntid=Integer.parseInt(item.getString("utf-8"));
          }
          
           if(fieldName.equals("nid")){
          out.println("<p>新闻编号："+item.getString("utf-8")+"</p>");
          nid=Integer.parseInt(item.getString("utf-8"));
          }
          
        
          if(fieldName.equals("ntitle")){
          out.println("<p>标题："+item.getString("utf-8")+"</p>");
          title=item.getString("utf-8");
          }
        
          if(fieldName.equals("nauthor")){
          out.println("<p>作者："+item.getString("utf-8")+"</p>");
          author=item.getString("utf-8");
          }
         
          if(fieldName.equals("nsummary")){
          out.println("<p>摘要："+item.getString("utf-8")+"</p>");
          summary=item.getString("utf-8");
          }
        
           if(fieldName.equals("ncontent")){
          out.println("<p>内容："+item.getString("utf-8")+"</p>");
          content=item.getString("utf-8");
          }
        
          
          }else{
          String fileName=item.getName();
          if(fileName !=null && !fileName.equals("")){
          File fullFile=new File(item.getName());
          
          uploadFileName=fullFile.getName();
          
          String newFileName=newName(uploadFileName);
          File saveFile=new File(uploadFilePath,newFileName);
          item.write(saveFile);
         
          
          out.print("上传的文件名是："+newFileName);
          out.print("<img src='upload/"+newFileName+"'/>");
          npicPath="upload/"+newFileName;
          }

          
          }//else

      }//while
      
       MySqlNewsDaoImpl dao=new MySqlNewsDaoImpl();
             
              News cc=new News();
              cc.setNtid(ntid);cc.setNid(nid);cc.setNtitle(title);
              cc.setNauthor(author);cc.setNsummary(summary);
              cc.setNcontent(content);cc.setNpicPath(npicPath);
              
              int i=dao.updateNews(cc);
               if(i>0){
     %>
     <span>修改成功</span> <br>
     <a href="admin.jsp?opr=findNew&page=1">返回</a>
     <%
     }else{

      %>
           <span>修改失败</span>
           <br>
           <a href="admin.jsp?opr=findNew&page=1">返回</a>
           
      
      <% }
      
      
      }catch(Exception e){
        e.printStackTrace();
      }
      
      
      }//isMultipart
      
      
      
      
       %>
      
      
      </body>
</html>
